﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1___ComplexNumber
{
    class Program
    {
        class ComplexNumber
        {
            private double real;
            private double img;

            public ComplexNumber()
            {
                real = 0;
                img = 0;
            }

            public ComplexNumber(double a, double b)
            {
                real = a;
                img = b;
            }

            public double Real
            {
                get { return real; }
            }

            public double Img
            {
                get { return img; }
            }

            public static ComplexNumber operator +(ComplexNumber a, ComplexNumber b)
            {
                double x = a.Real + b.Real;
                double y = a.Img + b.Img;
                return new ComplexNumber(x, y);
            }

            public static ComplexNumber operator -(ComplexNumber a, ComplexNumber b)
            {
                double x = a.Real - b.Real;
                double y = a.Img - b.Img;
                return new ComplexNumber(x, y);
            }

            public static ComplexNumber operator *(ComplexNumber a, ComplexNumber b)
            {
                double x = a.Real * b.Real - a.Img * b.Img;
                double y = a.Img * b.Real + a.Real * b.Img;
                return new ComplexNumber(x, y);
            }

            public static ComplexNumber operator /(ComplexNumber a, ComplexNumber b)
            {
                double x = (a.Real * b.Real + a.Img * b.Img) / (b.Real * b.real + b.img * b.img);
                double y = (a.img * b.real - a.real * b.img) / (b.Real * b.real + b.img * b.img);
                return new ComplexNumber(x, y);
            }

            public override string ToString()
            {
                return string.Format("{0} + i{1}", this.Real, this.Img);
            }

            public static explicit operator double(ComplexNumber a)
            {
                return a.real;
            }

            public static implicit operator ComplexNumber(double a)
            {
                ComplexNumber result = new ComplexNumber(a, 0);
                return result;
            }

            public double this[int index]
            {
                get
                {
                    if (index == 0) return this.Img;
                    else if (index == 1) return this.real;
                    else return -1;
                }
            }
            
        }

        static void Main(string[] args)
        {
            ComplexNumber a = new ComplexNumber(3, 4);
            ComplexNumber b = new ComplexNumber(1, 2);

            Console.WriteLine(a+b);
            Console.WriteLine(a/b);
            Console.WriteLine(a[0]);
            Console.WriteLine(a[1]);
            double x = (double)a;
            Console.WriteLine(x);
            b = x;
            Console.WriteLine();
            Console.WriteLine(b);
        }
    }
}
